/***************************************************************************
 *   Copyright (C) 2008/2009/2012/2014 by Philipp Müller                   *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include "channellist.h"
#include "vdr.h"

qint32 ChannelList::channelFromRow( int row ) const
{
    if ( ( row < 0 ) || ( row > count() - 1 ) )
    {
        return -1;
    }

    return at( row ).channel;
}


int ChannelList::rowFromChannel( quint32 channel ) const
{
    for ( int i = 0; i < size(); ++i )
    {
        if ( at( i ).channel == channel )
        {
            return i;
        }
    }

    return -1;
}


int ChannelList::rowOfEpgItem( int row, QDateTime dateTime )
{
    if ( ( row < 0 ) || ( row > size() ) )
    {
        qDebug() << "ChannelList::rowOfEpgItem called, but channelList size:" << size() << "< requested row:" << row;
        return -1;
    }

    int listSize = at( row ).scheduleList.size();
    int scheduleNumber = -1;

    for ( int i = 0; i < listSize; ++i )
    {
        if ( at( row ).scheduleList[ i ].time                                                 <= dateTime &&
             at( row ).scheduleList[ i ].time.addSecs( at( row ).scheduleList[ i ].duration ) >  dateTime )
        {
            scheduleNumber = i;
            break;
        }
    }

    if ( scheduleNumber == -1 )
    {
        if ( listSize > 0 )
        {
            return 0;
        }
        else
        {
            return scheduleNumber;
        }
    }
    else
    {
        return scheduleNumber;
    }
}
